package com.nebula.controller.portal;

import java.lang.reflect.InvocationTargetException;
import java.util.Map;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSON;
import com.nebula.controller.BaseController;
import com.nebula.entity.User;
import com.nebula.common.Const;
import com.nebula.common.Result;
import com.nebula.service.IUserService;
import com.nebula.service.impl.UserServiceImpl;
import com.nebula.util.MD5Util;
import com.nebula.vo.UserVo;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * @author CcccX
 */
@WebServlet(name = "QuestionController", urlPatterns = "/question")
public class QuestionController extends BaseController {
    private static final long serialVersionUID = 1L;

    private IUserService userService = new UserServiceImpl();

    public String index(HttpServletRequest request, HttpServletResponse response) {
        String forward = (String) request.getSession().getAttribute("forward");
        String userId = request.getParameter("userId");
        if (StringUtils.isBlank(userId) || !forward.equals(MD5Util.MD5EncodeUtf8(userId))) {
            return Const.REDIRECT + "login.jsp";
        }
        request.setAttribute("userId", userId);
        return Const.FORWARD + Const.DEFAULT_URL + "register2.jsp";
    }

    public String indexToQa(HttpServletRequest request, HttpServletResponse response) {
        return Const.FORWARD + Const.DEFAULT_URL + "updateQa.jsp";
    }

    public String register(HttpServletRequest request, HttpServletResponse response) throws InvocationTargetException, IllegalAccessException {
        Map<String, String[]> map = request.getParameterMap();

        User user = new User();
        BeanUtils.populate(user, map);
        try {
            if (StringUtils.isBlank(user.getId())) {
                return Const.JSON + JSON.toJSONString(Result.createByErrorMessage("用户ID不能为空"));
            }
            return getUpdateResult(user);
        } catch (Exception e) {
            e.printStackTrace();
            return Const.JSON + JSON.toJSONString(Result.createByErrorMessage(Const.PROGRAM_EXCEPTION));
        }
    }

    public String updateQa(HttpServletRequest request, HttpServletResponse response) {
        Map<String, String[]> map = request.getParameterMap();
        UserVo userVo = (UserVo) request.getSession().getAttribute(Const.USER_INFO);
        try {
            User user = new User();
            BeanUtils.populate(user, map);
            user.setId(userVo.getId());
            return getUpdateResult(user);
        } catch (Exception e) {
            e.printStackTrace();
            return Const.JSON + JSON.toJSONString(Result.createByErrorMessage(Const.PROGRAM_EXCEPTION));
        }
    }

    private String getUpdateResult(User user) {
        if (user.getQuestionOne() == null || user.getQuestionOne() == -1) {
            return Const.JSON + JSON.toJSONString(Result.createByErrorMessage("请选择安全问题1"));
        }
        if (StringUtils.isBlank(user.getAnswerOne())) {
            return Const.JSON + JSON.toJSONString(Result.createByErrorMessage("请输入安全问题1的答案"));
        }

        if (user.getQuestionTwo() == null || user.getQuestionTwo() == -1) {
            return Const.JSON + JSON.toJSONString(Result.createByErrorMessage("请选择安全问题2"));
        }
        if (StringUtils.isBlank(user.getAnswerTwo())) {
            return Const.JSON + JSON.toJSONString(Result.createByErrorMessage("请输入安全问题2的答案"));
        }

        if (user.getQuestionThree() == null || user.getQuestionThree() == -1) {
            return Const.JSON + JSON.toJSONString(Result.createByErrorMessage("请选择安全问题3"));
        }
        if (StringUtils.isBlank(user.getAnswerThree())) {
            return Const.JSON + JSON.toJSONString(Result.createByErrorMessage("请输入安全问题3的答案"));
        }

        if (user.getQuestionOne().equals(user.getQuestionTwo()) || user.getQuestionOne().equals(user.getQuestionThree())
                || user.getQuestionTwo().equals(user.getQuestionThree())) {
            return Const.JSON + JSON.toJSONString(Result.createByErrorMessage("安全问题不能重复"));
        }
        user.setAnswerOne(MD5Util.MD5EncodeUtf8(user.getAnswerOne().trim()));
        user.setAnswerTwo(MD5Util.MD5EncodeUtf8(user.getAnswerTwo().trim()));
        user.setAnswerThree(MD5Util.MD5EncodeUtf8(user.getAnswerThree().trim()));
        userService.update(user);
        return Const.JSON + JSON.toJSONString(Result.createBySuccessMessage("保存成功"));
    }
}
